package store

import (
	"context"
	"fmt"
	baseStore "gitee.com/zaiqiang231/go-base-app/base_app/store"
	"github.com/go-redis/redis/v8"
	"strconv"
	"time"
)

const (
	CollectKey = "CampCollectKey:%v"
)

func SetCollectCache(ctx context.Context, uid uint64, campid int64) error {
	key := fmt.Sprintf(CollectKey, uid)
	redisStore := baseStore.GetRedisStore()
	_, err := redisStore.ZAdd(ctx, key, &redis.Z{
		Score:  float64(time.Now().Unix()), // 成员的分值
		Member: campid,                     // 成员的值
	}).Result()
	if err != nil {
		return err
	}
	return nil
}

func RemCollectCache(ctx context.Context, uid int64, campid int64) error {
	key := fmt.Sprintf(CollectKey, uid)
	redisStore := baseStore.GetRedisStore()
	_, err := redisStore.ZRem(ctx, key, campid).Result()
	if err != nil {
		return err
	}
	return nil
}

func IsCollect(ctx context.Context, uid int64, campid int64) (bool, error) {
	key := fmt.Sprintf(CollectKey, uid)
	redisStore := baseStore.GetRedisStore()
	ret, err := redisStore.ZScore(ctx, key, strconv.FormatInt(campid, 10)).Result()
	if err != nil {
		return false, err
	}
	if ret > 0 {
		return true, nil
	}
	return false, nil
}
